package com.zhangxin.threadcoreknowledge.sixstates;

public class Demo02BlockedWaitngTimedWaiting implements Runnable{

    public static void main(String[] args) {
        Demo02BlockedWaitngTimedWaiting r = new Demo02BlockedWaitngTimedWaiting();
        Thread thread1 = new Thread(r);
        thread1.start();
        Thread thread2 = new Thread(r);
        thread2.start();
        try {
            Thread.sleep(5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(thread1.getState()); // TIMED_WAITING  (因为正在执行28行的代码)
        System.out.println(thread2.getState()); // BLOCKED  (因为thread1拿到了锁)
        try {
            Thread.sleep(1300);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(thread1.getState()); // WAITING (因为执行了wait() )
    }


    @Override
    public void run() {
        syn();
    }

    private synchronized void syn() {
        try {
            Thread.sleep(1000);
            wait();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}
